//
//  OrdermessageView.m
//  PurchasingMeeting
//
//  Created by yanseng.lin on 3/24/14.
//  Copyright (c) 2014 YX. All rights reserved.
//

#import "OrdermessageView.h"
#import "ShopObject.h"
#import "DataManager.h"
#import "UIHelper.h"
#import "config.h"
#import "PMGlobal.h"
#import "AppManager.h"
@implementation OrdermessageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ordernumrefresh:) name:@"ordernumrefresh" object:nil];
//        UIImage *img = [[UIHelper getInstance] getBundleImage:@"main_message.png"];
//        self.frame = CGRectMake(0, 0, img.size.width, img.size.height);
        self.frame = CGRectMake(0, 0, 1024, 30);
        if ([[[PMGlobal getInstance]gettype]isEqualToString:@"type1"]) {
            self.backgroundColor = [UIColor colorWithRed:101/255.0 green:36/255.0 blue:0 alpha:1];
        }
        else
        {
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
        }
//        UIImageView *imageview = [[UIImageView alloc]initWithImage:img];
//        imageview.frame = CGRectMake(0, 0, img.size.width, img.size.height);
//        imageview.alpha = 0.5;
//        // imageview.center = CGPointMake(850, 40);
//        [self addSubview:imageview];

        
        self.totalprice = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, self.height)];
        self.totalprice.backgroundColor = [UIColor clearColor];

        self.totalprice.textColor = [UIColor whiteColor];
        [self addSubview:self.totalprice];
        
        self.totalcount = [[UILabel alloc]initWithFrame:CGRectMake(200, 0, 170, self.height)];
        self.totalcount.backgroundColor = [UIColor clearColor];

        self.totalcount.textColor = [UIColor whiteColor];
        [self addSubview:self.totalcount];
        
        UserObject *userobj = [[DataManager sharedManager]getUserObject];
        self.username = [[UILabel alloc]initWithFrame:CGRectMake(400, 0, 180, self.height)];
        self.username.backgroundColor = [UIColor clearColor];
        self.username.text = [NSString stringWithFormat:@"用户名:%@(%@)",userobj.userCode,[[DataManager sharedManager]getUserDesc]];
        self.username.textColor = [UIColor whiteColor];
        self.username.adjustsFontSizeToFitWidth = YES;
        [self addSubview:self.username];
        
        self.currentshop = [[UILabel alloc]initWithFrame:CGRectMake(self.username.right + 10, 0, 160, self.height)];
        self.currentshop.backgroundColor = [UIColor clearColor];
        self.currentshop.textColor = [UIColor whiteColor];
        self.currentshop.adjustsFontSizeToFitWidth = YES;
        [self addSubview:self.currentshop];
        
        self.submitlasttime = [[UILabel alloc]initWithFrame:CGRectMake(self.currentshop.right, 0, 200, self.height)];
        self.submitlasttime.right = 1024-5;
        self.submitlasttime.textAlignment = NSTextAlignmentRight;
        self.submitlasttime.backgroundColor = [UIColor clearColor];
        self.submitlasttime.adjustsFontSizeToFitWidth = YES;
        self.submitlasttime.textColor = [UIColor whiteColor];
        [self addSubview:self.submitlasttime];
        
        //[self ordernumrefresh:nil];
    }
    return self;
}

#pragma mark 更新订单数量
-(void)ordernumrefresh:(NSNotification *)sender
{
    if (bLookup_) {
        return;
    }
    bLookup_ = YES;
//    dispatch_queue_t refresh_queue = dispatch_queue_create("com.yx.pm.refresh", NULL);
//    dispatch_async(refresh_queue, ^(void) {
    
        ShopObject *shopobj = [[DataManager sharedManager]getCurrentShop];
        NSString *shopStr;
        if (shopobj) {
            //shopobj.shopID
            shopStr = [NSString stringWithFormat:@"当前店铺:%@",[shopobj getShopDescription]];
        }else{
            shopStr = [NSString stringWithFormat:@"当前店铺:"];
        }
        
        
        NSMutableDictionary *dic = [[DataManager sharedManager]getAllOrderSum:shopobj.shopID];
    
        NSString *count=[dic objectForKey:@"cnt"];
        if (!count) {
            count = @"0";
        }
        NSString *price=[dic objectForKey:@"totalmoney"];
        if (!price) {
            price = @"0";
        }
        NSString *priceStr = [NSString stringWithFormat:@"订单总金额:%@",price];

        NSString *orderStr = [NSString stringWithFormat:@"订单总数量:%@",count];

        NSString *submitlasttime = [[DataManager sharedManager]getSubmitLastTime];
        NSString *str = [NSString stringWithFormat:@"提交订单时间:%@",submitlasttime];
        
        
//        dispatch_async(dispatch_get_main_queue(), ^{
            self.currentshop.text = shopStr;
            self.submitlasttime.text = str;
            self.totalcount.text = orderStr;
            self.totalprice.text = priceStr;
            bLookup_ = NO;
            [[NSNotificationCenter defaultCenter]postNotificationName:@"dismissPopover" object:nil];
            
            [[AppManager sharedManager] resumeAutoSubmitSession];
            [[AppManager sharedManager] resumeAutoUpdateSession];
//        });
        
        
//    });
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
